/**
 * @author Shine
 */
package com.shine.crypt
{
	import com.shine.ports.IAnimationClip;
	import com.shine.utils.ClassUtil;
	import com.shine.utils.DisplayObjectUtil;
	import com.shine.utils.StringUtil;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class ShineClip extends BitmapMovie implements IAnimationClip
	{
		public var ratePercent:Number=1;
		
		protected var _length:int;
		protected var _isFill:Boolean;
		
		public function ShineClip(length:int=-1,isFill:Boolean=false)
		{
			super();
			
			_length=length;
			_isFill=isFill;
			
			if(_isFill && _length>0) addChild(null,_length);
		}
		
		public function get length():int
		{
			return _length;
		}
		
		protected function fill():void
		{
			if(!_isFill || _length<1) return;
			var n:uint=_length-_frames.length;
			while(n>0){
				_frames.push(0);
				n--;
			}
		}
		
		override protected function removeFromId(id:int):void
		{
			_cache[id]=null;
			
			for(var i:int=0; i<_frames.length; i++)
			{
				if(_frames[i] == id){
					if(_isFill){
						_frames[i] = 0;
					}else{
						_frames.splice(i, 1);
						i--;
					}
				}
			}
			
			addDirectPoint();
		}
		
		override public function removeChild(child:DisplayObject):void
		{
			super.removeChild(child);
			fill();
		}
		
		override public function removeChildAt(id:int):Boolean
		{
			var pass:Boolean=super.removeChildAt(id);
			if(pass)fill();
			return pass;
		}
		
		override public function removeAll():void
		{
			super.removeAll();
			if(_isFill && _length>0) addChild(null,_length);
		}
		
		override public function play(clockwise:Boolean=true):void
		{
			_clockwise=clockwise;
			stop();
			
			_myTimer = new Timer(uint(Math.abs(AnimationClip.interval*ratePercent)),0);
			_myTimer.addEventListener(TimerEvent.TIMER,onEnterFrame);
			_myTimer.start();
		}
		
		public function addChildAt(child:DisplayObject,position:uint,frames:uint=1):Array
		{
			if(position<1) return [];
			check_MC_label(child);
			
			var i:uint;
			var id:uint;
			var arr:Array=new Array();
			
			if(!_cacheAsBitmap || (child is Bitmap)){
				id=_cache.push(child);
				for(i=0;i<frames;i++){
					_frames[(position+i-1)]=(id-1);
				}
				if(child!=null)resetSizeValue(child);
				addDirectPoint();
				return [(id-1)];
			}else{
				if(child!=null){
					var list:Vector.<Bitmap>=changeToBitmap(child);
					var count:uint=0;
					var frameNum:uint=position-1;
					for(var j:uint=0;j<list.length;j++){
						id=_cache.push(list[j]);
						arr.push(id-1);
						
						resetSizeValue(list[j]);
						if(child is MovieClip){
							_frames[frameNum]=(id-1);
							frameNum++;
							count++;
							
							if(frames>list.length && count>=list.length){
								frames-=list.length;
							}else{
								continue;
							}
						}
						
						for(i=0;i<frames;i++){
							_frames[(frameNum+i)]=(id-1);
						}
					}
				}else{
					id=_cache.push(child);
					for(i=0;i<frames;i++){
						_frames[(position+i-1)]=(id-1);
					}
					arr.push(id-1);
				}
				addDirectPoint();
			}
			return arr;
		}
		
		override protected function addDirectPoint():void
		{
			firmClipLength();
			super.addDirectPoint();
		}
		
		protected function firmClipLength():void
		{
			if(_length>0 && _frames.length>_length){
				_frames.splice(_length,(_frames.length-_length));
			}
		}
		
		override public function clone(C:Class=null):IAnimationClip
		{
			if(!_cacheAsBitmap) return super.clone(ShineClip);
			
			var copy:ShineClip=new ShineClip(_length);
			copy.cacheAsBitmap=true;
			copy.ratePercent=ratePercent;
			copy.loop=loop;
			DisplayObjectUtil.copyAttribute(copy.container,container);
			
			for(var i:uint=0;i<_cache.length;i++){
				var l:int=getTargetLength(i);
				if(l!=-1){
					var item:Bitmap=_cache[i] as Bitmap;
					if(item==null){
						copy.addChild(item,l);
					}else{
						var child:Bitmap=new Bitmap(item.bitmapData.clone(),"auto",true);
						DisplayObjectUtil.copyAttribute(child,item);
						copy.addChild(child,l);
					}
				}
			}
			return copy;
		}
		
		override public function toString():String
		{
			return ClassUtil.toString(this,"length","ratePercent","cacheAsBitmap","makeupValue","currentFrame","totalFrames","maxWidth","maxHeight","container","loop");
		}
	}
}